import axios from '@/utils/axios'

// 剪贴板创建参数
export interface CreateClipboardParams {
  content: string
  isEditable?: boolean
  expireTime?: string
  maxAccessCount?: number
  password?: string  // 访问密码（可选）
}

// 剪贴板创建响应
export interface CreateClipboardResponse {
  shareId: string
  adminPassword: string  // 管理员密码
  hasPassword: boolean
  content: string
  isEditable: boolean
  expireTime: string
  createdTime: string
  updatedTime: string
  accessCount: number
  maxAccessCount?: number
  remainingHours: number
  isExpired: boolean
}

// 剪贴板数据
export interface ClipboardData {
  shareId: string
  hasPassword: boolean
  accessPassword?: string  // 访客密码（从/get接口返回）
  adminPassword?: string   // 管理员密码（从/get接口返回）
  content: string
  isEditable: boolean
  expireTime: string
  createdTime: string
  updatedTime: string
  accessCount: number
  maxAccessCount?: number
  remainingHours: number
  isExpired: boolean
  isAdmin?: boolean  // 当前用户是否为管理员
}

// 获取剪贴板参数
export interface GetClipboardParams {
  shareId: string
  password?: string  // 访问密码或管理员密码
}

// 更新剪贴板参数
export interface UpdateClipboardParams {
  shareId: string
  password?: string  // 访问密码或管理员密码
  content: string
  isEditable?: boolean
}

// 修改访问密码参数
export interface ChangeAccessPasswordParams {
  shareId: string
  adminPassword: string
  newAccessPassword?: string
}

// 修改管理员密码参数
export interface ChangeAdminPasswordParams {
  shareId: string
  oldAdminPassword: string
  newAdminPassword: string
}

// 生成随机访问密码参数
export interface GenerateAccessPasswordParams {
  shareId: string
  adminPassword: string
}

// 生成随机管理员密码参数
export interface GenerateAdminPasswordParams {
  shareId: string
  oldAdminPassword: string
}

// 生成密码响应
export interface GeneratePasswordResponse {
  shareId: string
  newPassword: string
}

// 设置过期时间参数
export interface SetExpireTimeParams {
  shareId: string
  adminPassword: string
  expireTime: string
}

// 删除剪贴板参数
export interface DeleteClipboardParams {
  shareId: string
  adminPassword: string
}

// 创建剪贴板
export const createClipboard = async (params: CreateClipboardParams): Promise<CreateClipboardResponse> => {
  const response = await axios.post('/api/utility/clipboard/create', params)
  if (response.code === 200) {
    return response.data
  }
  throw new Error(response.message || '创建剪贴板失败')
}

// 获取剪贴板内容
export const getClipboard = async (params: GetClipboardParams): Promise<ClipboardData> => {
  const response = await axios.post('/api/utility/clipboard/get', params)
  if (response.code === 200) {
    return response.data
  }
  throw new Error(response.message || '获取剪贴板失败')
}

// 更新剪贴板内容
export const updateClipboard = async (params: UpdateClipboardParams): Promise<ClipboardData> => {
  const response = await axios.put('/api/utility/clipboard/update', params)
  if (response.code === 200) {
    return response.data
  }
  throw new Error(response.message || '更新剪贴板失败')
}

// 修改访问密码
export const changeAccessPassword = async (params: ChangeAccessPasswordParams): Promise<boolean> => {
  const response = await axios.put('/api/utility/clipboard/change-access-password', params)
  if (response.code === 200) {
    return response.data
  }
  throw new Error(response.message || '修改访问密码失败')
}

// 修改管理员密码
export const changeAdminPassword = async (params: ChangeAdminPasswordParams): Promise<boolean> => {
  const response = await axios.put('/api/utility/clipboard/change-admin-password', params)
  if (response.code === 200) {
    return response.data
  }
  throw new Error(response.message || '修改管理员密码失败')
}

// 生成随机访问密码
export const generateAccessPassword = async (params: GenerateAccessPasswordParams): Promise<GeneratePasswordResponse> => {
  const response = await axios.put('/api/utility/clipboard/generate-access-password', params)
  if (response.code === 200) {
    return response.data
  }
  throw new Error(response.message || '生成访问密码失败')
}

// 生成随机管理员密码
export const generateAdminPassword = async (params: GenerateAdminPasswordParams): Promise<GeneratePasswordResponse> => {
  const response = await axios.put('/api/utility/clipboard/generate-admin-password', params)
  if (response.code === 200) {
    return response.data
  }
  throw new Error(response.message || '生成管理员密码失败')
}

// 设置剪贴板过期时间
export const setExpireTime = async (params: SetExpireTimeParams): Promise<ClipboardData> => {
  const response = await axios.put('/api/utility/clipboard/set-expire', params)
  if (response.code === 200) {
    return response.data
  }
  throw new Error(response.message || '设置过期时间失败')
}

// 删除剪贴板
export const deleteClipboard = async (params: DeleteClipboardParams): Promise<boolean> => {
  const response = await axios.delete('/api/utility/clipboard/delete', {
    data: params  // DELETE请求需要用data传递请求体
  })
  if (response.code === 200) {
    return response.data
  }
  throw new Error(response.message || '删除剪贴板失败')
}

// 设置编辑权限参数
export interface SetEditableParams {
  shareId: string
  adminPassword: string
  isEditable: boolean
}

// 设置编辑权限
export const setEditable = async (params: SetEditableParams): Promise<ClipboardData> => {
  const response = await axios.put('/api/utility/clipboard/set-editable', params)
  if (response.code === 200) {
    return response.data
  }
  throw new Error(response.message || '设置编辑权限失败')
}

// 检查密码免费访问参数
export interface CheckPasswordFreeParams {
  shareId: string
}

// 检查密码免费访问
export const checkPasswordFree = async (params: CheckPasswordFreeParams): Promise<boolean> => {
  const response = await axios.post('/api/utility/clipboard/check-password-free', params)
  if (response.code === 200) {
    return response.data
  }
  throw new Error(response.message || '检查密码免费访问失败')
}